using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Bookvar.ObjectModel;
using Bookvar.ObjectModel.Collections;

namespace BookvarWpf.Windows
{
    /// <summary>
    /// Interaction logic for SampleWindow.xaml
    /// </summary>

    public partial class SwitchMaps
    {
        private MindmapCollection loadedMaps;
//        private MindMap currentMap;
        private int startViewPosition;

        public SwitchMaps(MindmapCollection loadedMaps)
        {
            InitializeComponent();
            this.loadedMaps = loadedMaps;
            List<Picture> thumbs = new List<Picture>();
            foreach (MindMap map in loadedMaps)
            {

                thumbs.Add(new Picture(map.MainTopic.Thumbnail));
            }
            lstMaps.ItemsSource = thumbs;
            lstMaps.SelectionChanged += lstMaps_SelectionChanged;

        }

        void lstMaps_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstMaps.SelectedIndex == startViewPosition + 4)
            {
                ScrollViewer viewer = FindVisualChild(lstMaps);
                if (viewer != null)
                {
                    viewer.ScrollToHorizontalOffset(viewer.HorizontalOffset + 200);
                }
                startViewPosition++;

            }
            else if (lstMaps.SelectedIndex == 0)
            {
                startViewPosition = 0;
                ScrollViewer viewer = FindVisualChild(lstMaps);
                if (viewer != null)
                {
                    viewer.ScrollToHorizontalOffset(0);
                }
            }

        }

        private static ScrollViewer FindVisualChild(DependencyObject obj)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                ScrollViewer viewer = child as ScrollViewer;
                if (viewer != null)
                {
                    return viewer;
                }

                viewer = FindVisualChild(child);
                if (viewer != null)
                {
                    return viewer;
                }
            }
            return null;
        }



        internal void MoveToNextMap()
        {
            if (lstMaps.SelectedIndex == lstMaps.Items.Count - 1)
            {
                lstMaps.SelectedIndex = 0;
            }
            else
            {
                lstMaps.SelectedIndex++;
            }
        }

        internal void SetCurrentMap(MindMap mindMap)
        {
//            currentMap = mindMap;
            for (int i = 0; i < loadedMaps.Count; i++)
            {
                MindMap map = loadedMaps[i];
                if (map.MainTopic.ID == mindMap.MainTopic.ID)
                {
                    lstMaps.SelectedIndex = i;
                    return;
                }
            }
        }
    }
}